package com.icim.util;

import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;

/** 
 *  
 *Module:       HttpClientUtil.java 
 *Description:  以get/post的方式发送数据到指定的http接口---利用httpclient.jar包---HTTP接口的调用 
 *Company:       
 *Author:       ptp 
 *Date:         Feb 22, 2012 
 */
public class HttpClientUtil {

	private static final Logger logger = Logger.getLogger(HttpClientUtil.class);

	private String url;
	private String healthCheckUrl;


	/** 
	 * post方式  
	 * @param param1  
	 * @param param2 
	 * @return 
	*/
	public String postHttp(String xml) {
		String responseMsg = "";

		//1.构造HttpClient的实例  
		HttpClient httpClient = new HttpClient();

		httpClient.getParams().setContentCharset("UTF-8");

		//2.构造PostMethod的实例  
		PostMethod postMethod = new PostMethod(url);

		//3.把参数值放入到PostMethod对象中  
		//方式1：  
		/*        NameValuePair[] data = { new NameValuePair("param1", param1), 
		                new NameValuePair("param2", param2) }; 
		        postMethod.setRequestBody(data);*/

		//方式2：      
		postMethod.addParameter("message", xml);

		try {
			// 4.执行postMethod,调用http接口  
			httpClient.executeMethod(postMethod);//200  

			//5.读取内容  
			responseMsg = postMethod.getResponseBodyAsString().trim();
			logger.info(responseMsg);

			//6.处理返回的内容  

		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//7.释放连接  
			postMethod.releaseConnection();
		}
		return responseMsg;
	}

	public void healthCheck() throws Exception {
		/** 
		 * post方式  
		 * @param param1  
		 * @param param2 
		 * @return 
		*/

		HttpClient httpClient = new HttpClient();
		httpClient.getParams().setContentCharset("UTF-8");
		PostMethod postMethod = new PostMethod(healthCheckUrl);

		// 4.执行postMethod,调用http接口  
		int executeMethod = httpClient.executeMethod(postMethod);//200  

		if(executeMethod!=200){
			throw new Exception("资料外送服务器 连接异常，请确认. url:[" + healthCheckUrl +"]");
		}
	}

	/** 
	 * 测试的main方法 
	 * @param args 
	 * @throws IOException 
	 * @throws HttpException 
	*/
	public static void main(String[] args) throws HttpException, IOException {
		HttpClient httpClient = new HttpClient();

		httpClient.getParams().setContentCharset("UTF-8");

		//2.构造PostMethod的实例  
		PostMethod postMethod = new PostMethod("http://localhost:8080/icim-data-importer/healthCheck");
		httpClient.setConnectionTimeout(200);
		httpClient.setTimeout(200);
		int executeMethod = httpClient.executeMethod(postMethod);
		System.out.println(executeMethod);
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getHealthCheckUrl() {
		return healthCheckUrl;
	}

	public void setHealthCheckUrl(String healthCheckUrl) {
		this.healthCheckUrl = healthCheckUrl;
	}
	

}